<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class site extends CI_Controller {

     public function __construct()
     {
            parent::__construct();

            // Only proceed if user has loggedin
            if($this->session->userdata('login_success') == false)
            {
                redirect('/login/', 'refresh');
                exit;
            }

            // Load required models
            $this->load->model('fs_databases');
            $this->load->model('fs_tables');
            $this->load->model('fs_form');
            $this->load->model("fs_save");
     }

	public function index()
	{
        // Show databases column
        $data['databases'] = $this->show_databases();

        $this->parser->parse('tmpl/head', $this->config->item("header"));
        $this->parser->parse('index', $data);
        $this->load->view('tmpl/footer');
	}

    public function dashboard($db_name)
    {
        // Check to see if there is a site selected
        if(empty($db_name) && $this->session->userdata('active_site') == false ) {
            show_error("No active site selected. <a href='/'>Back</a>", 500);
        } else {
            // Set new active site
            if(!empty($db_name)) {
                $this->session->set_userdata('active_site', $db_name);
            }

            // Show databases column
            $data['databases'] = $this->show_databases();

            // Active db and load tables
            $db_name = $this->session->userdata('active_site');
            $list_of_tables = $this->fs_tables->get_tree_with_records($db_name,10);

            // Parser data
            $data['dbname'] = $db_name;
            $data['tables'] = $list_of_tables;

            // Views
            $this->parser->parse('tmpl/head', $this->config->item("header"));
            $this->parser->parse('index', $data);
            $this->load->view('tmpl/footer');
        }
    }

    public function overview($table)
    {
       if(empty($table)) {
            show_error("No active table selected. <a href='/'>Back</a>", 500);
       } else {

            // Show databases column
            $data['databases'] = $this->show_databases();

            // Active db and load tables
            $db_name = $this->session->userdata('active_site');
            $list_of_tables = $this->fs_tables->get_tree_with_records($db_name,10);

            // Parser data
            $data['dbname'] = $db_name;
            $data['tables'] = $list_of_tables;

            // Generate overview
            $overview_data['table_name'] = $table;
            $overview_data['grid_width'] = ($this->session->userdata('interface_width')-28);
            $overview_data['grid_height'] = 500;
            $overview_data["id_field"] = "id";
            $overview_data["title"] = "Overview of ".$table;
            $overview_data['headings'] = $this->fs_tables->generate_grid_table($table,$overview_data['grid_width']);
            $data['content'] = $this->parser->parse('usercontrols/overview.php', $overview_data, TRUE);

            // Views
            $this->parser->parse('tmpl/head', $this->config->item("header"));
            $this->parser->parse('index', $data);
            $this->load->view('tmpl/footer');
       }
    }

    public function tableview($table,$id) {
       if(empty($table)) {
            show_error("No active table selected. <a href='/'>Back</a>", 500);
       } else {

           // New record?
           $id = (empty($id)) ? 0 : $id;

            // Show databases column
            $data['databases'] = $this->show_databases();

            // Active db and load tables
            $db_name = $this->session->userdata('active_site');
            $list_of_tables = $this->fs_tables->get_tree_with_records($db_name,10);

            // Parser data
            $data['dbname'] = $db_name;
            $data['tables'] = $list_of_tables;

            // Build the form
            $data['content'] = $this->fs_form->build_form($table,$id);

            // Views
            $this->parser->parse('tmpl/head', $this->config->item("header"));
            $this->parser->parse('index', $data);
            $this->load->view('tmpl/footer');
       }
    }

    // Will be triggered after saving a form
    public function save()
    {
       $form_table = $this->session->userdata('form_post_table');
       $form_record_id = $this->session->userdata('form_post_id');

        // Primary checks
        if( (strlen($form_table) == 0 || strlen($form_record_id) == 0) ||  $this->input->post('fs_unique_id') != $form_record_id ) {
            show_error("No valid table sessions found.", 500);
            exit;
        } else {

            $this->fs_save->save_data( $this->input->post() );
        }
    }
    

    private function show_databases()
    {
        $list_of_dbs = $this->fs_databases->get_databases_url();
        return $list_of_dbs;
    }

}

/* End of file site.php */
/* Location: ./application/controllers/site.php */